package it.unisannio.lesim.fsm;

import java.util.Hashtable;
import java.util.StringTokenizer;

/**
 *
 * @author Marco Calisti
 * @author Antonio Cuomo
 * @author Rocco Mazzeo
 * @author Francesco Pacilio
 * 
 */

public class Message {

        private int no;
    private String buff;
    private long sec;
    private long usec;
    private int sourceIp;
    private int sourcePort;
    private int desIp;
    private int desPort;
    private int workNum;
    private int agentNum;
    private int protocolType;    // Field estratti dal campo Buff
    private String callId = "0";
    private int cseqNumber = 0;
    private String cseqString = "0";
    private int expires = 0;
    private String type = "0"; // INVITE,ACK,BYE,REGISTER
    private String dest = "0"; // sip:bob@hostname.com
    private int numResp = 0; // 200
    private String nameResp = "0"; // OK
    private String firstRow = "0";
    private  String from,  to ;    //Field del messaggio
    
    public int getAgentNum() {
        return agentNum;
    }

    public String getBuff() {
        return buff;
    }

    public int getDesIp() {
        return desIp;
    }

    public int getDesPort() {
        return desPort;
    }

    public int getNo() {
        return no;
    }

    public int getProtocolType() {
        return protocolType;
    }

    public long getSec() {
        return sec;
    }

    public int getSourceIp() {
        return sourceIp;
    }

    public int getSourcePort() {
        return sourcePort;
    }

    public long getUsec() {
        return usec;
    }

    public int getWorkNum() {
        return workNum;
    }

    public void setAgentNum(int agentNum) {
        this.agentNum = agentNum;
    }

    public void setBuff(String buff) {
        this.buff = buff;

        String[] buffer = buff.split("\n");
        setFirstRow(buffer[0]);
        StringTokenizer st = new StringTokenizer(buffer[0]);
        String first = st.nextToken();
        if (first.contains("SIP")) {
            numResp = Integer.parseInt(st.nextToken());
            nameResp = st.nextToken();
            type = nameResp;
        // messaggio di risposta
        } else {
            //messaggio di richiesta
            type = first;
            dest = st.nextToken();
        }

        for (int i = 1; i < buffer.length; i++) {
            int separator = buffer[i].indexOf(':');
            if (separator != -1) {
                String key = buffer[i].substring(0, separator).trim();
                String valueAndOptions = buffer[i].substring(separator + 1).trim();
                StringTokenizer tok = new StringTokenizer(valueAndOptions, ";");
                String value = tok.nextToken();

                String[] options = new String[tok.countTokens()];
                for (int n = 0; n < options.length; n++) {
                    options[n] = tok.nextToken();
                }
                FieldValue fValue = new FieldValue(value, options);
                if (key.equals("From") || key.equals("To")) {

                }
                fields.put(key, fValue);
            }
            if (buffer[i].startsWith("Call-ID:")) {
                st = new StringTokenizer(buffer[i]);
                st.nextElement();
                callId = st.nextToken();
            }
            if (buffer[i].startsWith("CSeq:")) {
                st = new StringTokenizer(buffer[i]);
                st.nextElement();
                cseqNumber = Integer.parseInt(st.nextToken());
                cseqString = st.nextToken();
            }
            if (buffer[i].startsWith("Expires:")) {
                st = new StringTokenizer(buffer[i]);
                st.nextElement();
                expires = Integer.parseInt(st.nextToken());
            }
            if (buffer[i].startsWith("From:")) {
                st = new StringTokenizer(buffer[i]);
                st.nextElement();
                from = st.nextToken();
                from = from.substring(1);
                st = new StringTokenizer(from, ">");
                from = st.nextToken();
            }
            if (buffer[i].startsWith("To:")) {
                st = new StringTokenizer(buffer[i]);
                st.nextElement();
                to = st.nextToken();
                to = to.substring(1);
                st = new StringTokenizer(to, ">");
                to = st.nextToken();
            }
        }
    }

    public void setDesIp(int desIp) {
        this.desIp = desIp;
    }

    public void setDesPort(int desPort) {
        this.desPort = desPort;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setProtocolType(int protocolType) {
        this.protocolType = protocolType;
    }

    public void setSec(long sec) {
        this.sec = sec;
    }

    public void setSourceIp(int sourceIp) {
        this.sourceIp = sourceIp;
    }

    public void setSourcePort(int sourcePort) {
        this.sourcePort = sourcePort;
    }

    public void setUsec(long usec) {
        this.usec = usec;
    }

    public void setWorkNum(int workNum) {
        this.workNum = workNum;
    }

    public Hashtable getFields() {
        return fields;
    }
    private Hashtable fields = new Hashtable();

    public FieldValue getField(String name) {
        return (FieldValue) fields.get(name);
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getCallId() {
        return callId;
    }

    public void setCallId(String callId) {
        this.callId = callId;
    }

    public int getCseqNumber() {
        return cseqNumber;
    }

    public void setCseqNumber(int cseqNumber) {
        this.cseqNumber = cseqNumber;
    }

    public String getCseqString() {
        return cseqString;
    }

    public void setCseqString(String cseqString) {
        this.cseqString = cseqString;
    }

    public String getDest() {
        return dest;
    }

    public void setDest(String dest) {
        this.dest = dest;
    }

    public int getExpires() {
        return expires;
    }

    public void setExpires(int expires) {
        this.expires = expires;
    }

    public String getNameResp() {
        return nameResp;
    }

    public void setNameResp(String nameResp) {
        this.nameResp = nameResp;
    }

    public int getNumResp() {
        return numResp;
    }

    public void setNumResp(int numResp) {
        this.numResp = numResp;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getFirstRow() {
        return firstRow;
    }

    public void setFirstRow(String firstRow) {
        this.firstRow = firstRow;
    }
}
